
import router from '@/router';
import { post } from '@/utils/request';
import {defineStore} from 'pinia';
import Cookies from "js-cookie";
import { ElMessage } from 'element-plus';


export const useUserStore = defineStore('useUserStore',{
    persist:true, //持久化
    state:()=>({
        user:new userInfo(),//用户信息
        userpassword:{
            username:'',
            password:'',
        }
    }),
    getters:{
        PermissionList:(state)=>{
            return state.user.perms
        }
    },
    actions:{
        // 设置用户信息
        setUser(user:userInfo){
            this.user = user
        },
        //退出登录
         Loginout(){
            const data = {
                _gp:'admin',
                _mt:'logout'
            }
            post(data).then(res=>{
                let {errno,errmsg} = res
               if(200===errno){
                this.user=null
                Cookies.remove("token")
                router.replace('/login')
               setTimeout(() => {
                ElMessage.success("退出成功")
               },5000);
               }else{
                router.replace('/login')
                ElMessage.error(errmsg)
               }
            })
         }
    }
})

class userInfo {
    avatarUrl?:string;
    gmtCreate:number;
    gmtLastLogin:string;
    gmtUpdate:number;
    id:number;
    lastLoginIp:string;
    perms:string[];
    phone:string;
    realname:string;
    roleIds:number[];
    roles:string[];
    status:number;
    username:string;
    constructor() {
        this.avatarUrl="";
        this.gmtCreate = 0;
        this.gmtLastLogin = "";
        this.gmtUpdate = 0;
        this.id = 0;
        this.lastLoginIp = "";
        this.perms = [];
        this.phone = "";
        this.realname = "";
        this.roleIds = [];
        this.roles = [];
        this.status = 0;
        this.username = "";
    }
}

